﻿using System;
using GrainManage.Base;
using GrainManage.Entities.Filter;
using GrainManage.Entities.Trade;
using GrainManage.Tools;
using GrainManage.Api;
using GrainManage.Proxy;

namespace GrainManage.Pages
{
    /// <summary>
    /// Interaction logic for TradePage.xaml
    /// </summary>
    public partial class TradePage : BasePage
    {
        public TradePage()
        {
            InitializeComponent();

            RegisterService();

            BindQueryEvent();

            BindDropDownEvent();

            BindCheckedEvent();

            myPager.StartService(CurrentService);
        }

        private string CurrentService = TradeApi.GetDetail;

        private void RegisterService()
        {
            myPager.RegisterService((pageIndex, pageSize) =>
            {
                var filter = GetFilter();
                TryBeginInvoke(() =>
                {
                    var input = new InputGetDetail()
                    {
                        Authentication = App.Authentication,
                        TradeFilter = filter,
                        PageIndex = pageIndex,
                        PageSize = pageSize
                    };
                    return Creator.TradeProxy.GetDetail(input);
                }, (result) =>
                {
                    UI.ShowContent(listview, result.TradeDetail);
                    myPager.TotalRowCount = result.Total;
                });
            }, TradeApi.GetDetail);

            myPager.RegisterService((pageIndex, pageSize) =>
            {
                var filter = GetFilter();

                TryBeginInvoke(() =>
                {
                    var input = new InputGetTotal()
                    {
                        Authentication = App.Authentication,
                        Grain = filter.Grain,
                        TradeType = filter.TradeType,
                        StartTime = filter.StartTime,
                        EndTime = filter.EndTime,
                        PageIndex = pageIndex,
                        PageSize = pageSize
                    };
                    return Creator.TradeProxy.GetTotal(input);
                }, (result) =>
                {
                    UI.ShowContent(listview, result.TotalDetail);
                    myPager.TotalRowCount = result.Total;
                });
            }, TradeApi.GetTotal);

            myPager.RegisterService((pageIndex, pageSize) =>
            {
                var filter = GetFilter();

                TryBeginInvoke(() =>
                {
                    var input = new InputGetTotalByArea()
                    {
                        Authentication = App.Authentication,
                        Area = filter.Area,
                        Grain = filter.Grain,
                        TradeType = filter.TradeType,
                        StartTime = filter.StartTime,
                        EndTime = filter.EndTime,
                        PageIndex = pageIndex,
                        PageSize = pageSize
                    };
                    return Creator.TradeProxy.GetTotalByArea(input);
                }, (result) =>
                {
                    UI.ShowContent(listview, result.TotalDetail);
                    myPager.TotalRowCount = result.Total;
                });
            }, TradeApi.GetTotalByArea);

            myPager.RegisterService((pageIndex, pageSize) =>
            {
                var filter = GetFilter();

                TryBeginInvoke(() =>
                {
                    var input = new InputGetTotalByContactArea()
                    {
                        Authentication = App.Authentication,
                        TradeFilter = filter,
                        PageIndex = pageIndex,
                        PageSize = pageSize
                    };
                    return Creator.TradeProxy.GetTotalByContactArea(input);
                }, (result) =>
                {
                    UI.ShowContent(listview, result.TotalDetail);
                    myPager.TotalRowCount = result.Total;
                });
            }, TradeApi.GetTotalByContactArea);
        }

        private TradeFilter GetFilter()
        {
            var filter = new TradeFilter();
            filter.TradeType = cbTradeType.Text.Trim(' ');
            filter.Grain = cbGrain.Text.Trim(' ');
            filter.ContactName = cbContactName.Text.Trim(' ');
            filter.Area = cbArea.Text.Trim(' ');
            filter.StartTime = Convertor.ToDateTime(timeStart.Text.Trim(' '));
            filter.EndTime = Convertor.ToDateTime(timeEnd.Text.Trim(' '));
            return filter;
        }

        private void BindCheckedEvent()
        {
            var eh = new GrainManage.Tools.EventHelper();
            eh.AddCheckedEventHandler(rbDetail, () => { CurrentService = TradeApi.GetDetail; myPager.StartService(CurrentService); });
            eh.AddCheckedEventHandler(rbTotal, () => { CurrentService = TradeApi.GetTotal; myPager.StartService(CurrentService); });
            eh.AddCheckedEventHandler(rbTotalArea, () => { CurrentService = TradeApi.GetTotalByArea; myPager.StartService(CurrentService); });
            eh.AddCheckedEventHandler(rbTotalAreaContact, () => { CurrentService = TradeApi.GetTotalByContactArea; myPager.StartService(CurrentService); });
        }

        private void BindDropDownEvent()
        {
            var eh = new GrainManage.Tools.EventHelper();
            //*****************************************填充联系人*******************************************
            eh.AddDropDownOpenedHandler(cbContactName, () =>
            {
                TryBeginInvoke(() =>
                {
                    return Creator.ContactProxy.GetContactName(PageSettings.ContactNameCount);
                }, (result) =>
                {
                    UI.ShowContent(cbContactName, result);
                });
            });

            //*****************************************填充地址*******************************************
            eh.AddDropDownOpenedHandler(cbArea, () =>
            {
                TryBeginInvoke(() =>
                {
                    return Creator.ContactProxy.GetContactArea(PageSettings.ContactAreaCount);
                }, (result) =>
                {
                    UI.ShowContent(cbArea, result);
                });
            });

            //*****************************************填充农作物*******************************************
            eh.AddDropDownOpenedHandler(cbGrain, () =>
            {
                TryBeginInvoke(() =>
                {
                    return Creator.PriceProxy.GetGrainType(PageSettings.GrainTypeCount);
                }, (result) =>
                {
                    UI.ShowContent(cbGrain, result);
                });
            });

            //*****************************************填充交易类型*******************************************
            eh.AddDropDownOpenedHandler(cbTradeType, () =>
            {
                TryBeginInvoke(() =>
                {
                    return Creator.TradeProxy.GetTradeType(PageSettings.TradeTypeCount);
                }, (result) =>
                {
                    UI.ShowContent(cbTradeType, result);
                });
            });
        }

        private void BindQueryEvent()
        {
            //*****************************************按键与双击事件*******************************************
            Action cbQueryAction = () => myPager.StartService(CurrentService);
            var eh = new GrainManage.Tools.EventHelper();
            eh.AddQueryEventHandler(cbTradeType, cbQueryAction);
            eh.AddQueryEventHandler(cbContactName, cbQueryAction);
            eh.AddQueryEventHandler(cbArea, cbQueryAction);
            eh.AddQueryEventHandler(cbGrain, cbQueryAction);
            eh.AddQueryEventHandler(timeStart, cbQueryAction);
            eh.AddQueryEventHandler(timeEnd, cbQueryAction);
        }
    }
}
